package com.example.java_chatroom.api;

import com.example.java_chatroom.component.CommonSession;
import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.FriendMapper;
import com.example.java_chatroom.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 朋友api
 */
@Slf4j
@RestController
public class FriendAPI {

    /**
     * 朋友表的mapper
     */
    @Resource
    private FriendMapper friendMapper;

    /**
     * session公共服务类
     */
    @Resource
    private CommonSession commonSession;

    /**
     * 获取朋友列表
     * @param req       HttpServletRequest
     * @return      返回一个朋友信息列表
     */
    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req) {
        // 1. 先从会话中, 获取到 userId 是啥.
        User user = commonSession.getUserBySession(req, "getFriendList");
        // 如果user为null，则直接返回
        if (null == user) {
            return new ArrayList<Friend>();
        }
        // 2. 根据 userId 从数据库查数据即可
        return friendMapper.selectFriendList(user.getUserId());
    }
}
